var mainType = '',status = 10
$(document).ready(function(){
    mainType = global.mainType
    getOrdersList1(ordersListPageNo1,showNum)
    getOrdersList2(ordersListPageNo2,showNum)
    getOrdersList3(ordersListPageNo3,showNum)
})

//切换状态
$('#order_status').on('change',function(){
    status = $(this).children('option:selected').val();
})

//导航切换
$('.nav-item').on('click',function(){
    $('.nav-item').removeClass('active');
    $(this).addClass('active');
    var index = $(this).index();
    // console.log(index);
    $('.ordersListTable').css('display','none');
    var itemBox = document.getElementsByClassName('ordersListTable');
    itemBox[index].style.display = 'block';
})

$('#searchBtn').on('click',function(){
    p = 1
    getOrdersList1(p,showNum);
    getOrdersList2(p,showNum);
    getOrdersList3(p,showNum);
})

/*** 健康商城订单 ***/
var ordersListPageNo1 = 1,showNum=10;
var ordersList1 = ''
function getOrdersList1(p,showNum){
    let time = '00:00:00'
    let endTime = '23:59:59'
    var start = $('#start').val() != ''? $('#start').val() + ' ' + time: '2019-01-01' + ' ' + time;//开始时间
    var end = $('#end').val() != ''? $('#end').val() + ' ' + endTime: laydate.now(0, 'YYYY-MM-DD') + ' ' + endTime;//结束时间
    start = Date.parse(start) / 1000
    end = Date.parse(end) / 1000
    if(mainType == '3'){
        mainType = 2
    }
    getAjax(`${global.apiUrl}/admin_get_health_shop_order`,function(res){//未完
        console.log(res);
        if(res == 0){
            var html = `
            <tr>
                <td colspan="7" align="center">请选择社区</td>
            </tr>`
            $('#ordersList1').html(html);
            return
        }
        if(res.list.length == 0 || !res.list){
            $("#page").paging({
                pageNo:0,
                totalPage: 0, //总页数
                totalSize: 0,
                callback: function(num) {
                    getOrdersList1(num,showNum)
                }
            });//未完
            var html = `
                    <tr>
                        <td colspan="7" align="center">暂无订单</td>
                    </tr>`
            $('#ordersList1').html(html);
        }else{
            $("#page").paging({
                pageNo:p,
                totalPage: Math.ceil(res.count/showNum), //总页数
                totalSize: res.count,
                callback: function(num) {
                    getOrdersList1(num,showNum)
                    ordersListPageNo1 = num
                }
            });
            var html = '';
            ordersList1 = res.list
            var list = res.list;
            for(var i in list){//未完
                html += `
                    <tr>
                        <td>${list[i].name}</td>
                        <td>${list[i].phone}</td>
                        <td>${list[i].all_order_num}</td>
                        <td>${setTime2(list[i].createtime)}</td>
                        <td>${list[i].all_price}</td>
                        <td>${getStatus(list[i].status)}</td>
                        <td class="control-td themeColor">
                            <span data-id="${list[i].id}" data-target="#OrdersModal1" data-toggle="modal" data-type='1'>查看详情</span> 
                        </td>
                    </tr>
            `
            }
            $('#ordersList1').html(html);
        }
    },{
        p:p,
        num:showNum,
        community_id: global.community_id,
        type: mainType,
        start_time: start,
        end_time: end,
        status: status
    })
}

/*** 社区商城订单 ***/
var ordersListPageNo2 = 1,showNum=10;
var ordersList2 = ''
function getOrdersList2(p,showNum){
    let time = '00:00:00'
    let endTime = '23:59:59'
    var start = $('#start').val() != ''? $('#start').val() + ' ' + time: '2019-01-01' + ' ' + time;//开始时间
    var end = $('#end').val() != ''? $('#end').val() + ' ' + endTime: laydate.now(0, 'YYYY-MM-DD') + ' ' + endTime;//结束时间
    start = Date.parse(start) / 1000
    end = Date.parse(end) / 1000
    if(mainType == '3'){
        mainType = 2
    }
    getAjax(`${global.apiUrl}/admin_get_service_shop_order`,function(res){//未完
        console.log(res);
        if(res == 0){
            var html = `
            <tr>
                <td colspan="7" align="center">请选择社区</td>
            </tr>`
            $('#ordersList2').html(html);
            return
        }
        if(res.list.length == 0){
            $("#page2").paging({
                pageNo:0,
                totalPage: 0, //总页数
                totalSize: 0,
                callback: function(num) {
                    getOrdersList2(num,showNum)
                }
            });//未完
            var html = `
                    <tr>
                        <td colspan="7" align="center">暂无订单</td>
                    </tr>`
            $('#ordersList2').html(html);
        }else{
            $("#page2").paging({
                pageNo:p,
                totalPage: Math.ceil(res.count/showNum), //总页数
                totalSize: res.count,
                callback: function(num) {
                    getOrdersList2(num,showNum)
                    ordersListPageNo2 = num
                }
            });
            var html = '';
            ordersList2 = res.list
            var list = res.list;
            for(var i in list){//未完
                html += `
                    <tr>
                        <td>${list[i].name}</td>
                        <td>${list[i].phone}</td>
                        <td>${list[i].order_num}</td>
                        <td>${setTime2(list[i].createtime)}</td>
                        <td>${list[i].order_price}</td>
                        <td>${getStatus(list[i].status)}</td>
                        <td class="control-td themeColor">
                            <span data-id="${list[i].id}" data-target="#OrdersModal1" data-toggle="modal" data-type='2'>查看详情</span> 
                        </td>
                    </tr>
            `
            }
            $('#ordersList2').html(html);
        }
    },{
        p:p,
        num:showNum,
        community_id: global.community_id,
        type: mainType,
        start_time: start,
        end_time: end,
        status: status
    })
}

/*** 特膳厨房订单 ***/
var ordersListPageNo3 = 1,showNum=10;
var ordersList3 = ''
function getOrdersList3(p,showNum){
    let time = '00:00:00'
    let endTime = '23:59:59'
    var start = $('#start').val() != ''? $('#start').val() + ' ' + time: '2019-01-01' + ' ' + time;//开始时间
    var end = $('#end').val() != ''? $('#end').val() + ' ' + endTime: laydate.now(0, 'YYYY-MM-DD') + ' ' + endTime;//结束时间
    start = Date.parse(start) / 1000
    end = Date.parse(end) / 1000
    if(mainType == '3'){
        mainType = 2
    }
    getAjax(`${global.apiUrl}/admin_get_service_kitchen_order`,function(res){//未完
        console.log(res);
        if(res == 0){
            var html = `
            <tr>
                <td colspan="7" align="center">请选择社区</td>
            </tr>`
            $('#ordersList3').html(html);
            return
        }
        if(res.list.length == 0){
            $("#page3").paging({
                pageNo:0,
                totalPage: 0, //总页数
                totalSize: 0,
                callback: function(num) {
                    getOrdersList3(num,showNum)
                }
            });//未完
            var html = `
                    <tr>
                        <td colspan="7" align="center">暂无订单</td>
                    </tr>`
            $('#ordersList3').html(html);
        }else{
            $("#page3").paging({
                pageNo:p,
                totalPage: Math.ceil(res.count/showNum), //总页数
                totalSize: res.count,
                callback: function(num) {
                    getOrdersList3(num,showNum)
                    ordersListPageNo3 = num
                }
            });
            var html = '';
            ordersList3 = res.list
            var list = res.list;
            for(var i in list){//未完
                html += `
                    <tr>
                        <td>${list[i].name}</td>
                        <td>${list[i].phone}</td>
                        <td>${list[i].order_num}</td>
                        <td>${setTime2(list[i].createtime)}</td>
                        <td>${list[i].order_price}</td>
                        <td>${getStatus(list[i].status)}</td>
                        <td class="control-td themeColor">
                            <span data-id="${list[i].id}" data-target="#OrdersModal1" data-toggle="modal" data-type='3'>查看详情</span> 
                        </td>
                    </tr>
            `
            }
            $('#ordersList3').html(html);
        }
    },{
        p:p,
        num:showNum,
        community_id: global.community_id,
        type: mainType,
        start_time: start,
        end_time: end,
        status: status
    })
}

$('#OrdersModal1').on('show.bs.modal',function(event){//未完
    var modal = $(this);
    var btnThis = $(event.relatedTarget);
    var type = btnThis.data('type');
    modal.find("#logisticsName2").css('display','none')
    modal.find("#logisticsNum2").css('display','none')
    modal.find(".logistics_name").text('')
    modal.find(".logistics_number").text('')
    var ordersId = btnThis.data('id');
    var ordersInfo = '';
    var list = []
    if(type == '1'){
        //健康商城
        for(var i in ordersList1){
            if(ordersList1[i].id == ordersId){
                ordersInfo = ordersList1[i]
            }
        }
        var html = ''
        var number = 0
        list = ordersInfo.product_content
        for(var j in list){
            number += Number(list[j].num)
            html += `<div class="shopItemAre">
                        <img src="${list[j].head_img}" class="shopImg"></img>
                        <div class="shopItem">
                            <div>${list[j].product_name}</div>
                            <div style="margin-top: 10px;">${'x'+ list[j].num}</div>
                        </div>
                    </div>`
        }
        $(".shopMainAre").html(html)
        $('.number').text(number)
        $('.price').text(ordersInfo.all_price)
        $('.orderNum').text(ordersInfo.all_order_num)
        $('.orderDate').text(setTime2(ordersInfo.createtime))
        $('.orderStatus').text(getStatus(ordersInfo.status))
        $('.name').text(ordersInfo.name)
        $('.phone').text(ordersInfo.phone)
        $('.address').text(ordersInfo.sheng + ordersInfo.shi + ordersInfo.qu + ordersInfo.address)
        if(ordersInfo.address_order_shop){
            $('#logisticsName2').css('display','block')
            $('.logistics_name').text(ordersInfo.address_order_shop)
        }
        if(ordersInfo.address_order_num){
            $('#logisticsNum2').css('display','block')
            $('.logistics_number').text(ordersInfo.address_order_num)
        }
    }else if(type == '2'){
        //社区商城
        for(var i in ordersList2){
            if(ordersList2[i].id == ordersId){
                ordersInfo = ordersList2[i]
            }
        }
        var html = ''
        var number = 0
        list = ordersInfo.product_content
        for(var j in list){
            number += Number(list[j].num)
            html += `<div class="shopItemAre">
                        <img src="${list[j].head_img}" class="shopImg"></img>
                        <div class="shopItem">
                            <div>${list[j].product_name}</div>
                            <div style="margin-top: 10px;">${'x'+ list[j].num}</div>
                        </div>
                    </div>`
        }
        $(".shopMainAre").html(html)
        $('.number').text(number)
        $('.price').text(ordersInfo.order_price)
        $('.orderNum').text(ordersInfo.order_num)
        $('.orderDate').text(setTime2(ordersInfo.createtime))
        $('.orderStatus').text(getStatus(ordersInfo.status))
        $('.name').text(ordersInfo.name)
        $('.phone').text(ordersInfo.phone)
        $('.address').text(ordersInfo.sheng + ordersInfo.shi + ordersInfo.qu + ordersInfo.address)
    }else if(type == '3'){
        for(var i in ordersList3){
            if(ordersList3[i].id == ordersId){
                ordersInfo = ordersList3[i]
            }
        }
        var html = ''
        var number = 0
        list = ordersInfo.product_content
        for(var j in list){
            number += Number(list[j].num)
            html += `<div class="shopItemAre">
                        <img src="${list[j].head_img}" class="shopImg"></img>
                        <div class="shopItem">
                            <div>${list[j].product_name}</div>
                            <div style="margin-top: 10px;">${'x'+ list[j].num}</div>
                        </div>
                    </div>`
        }
        $(".shopMainAre").html(html)
        $('.number').text(number)
        $('.price').text(ordersInfo.order_price)
        $('.orderNum').text(ordersInfo.order_num)
        $('.orderDate').text(setTime2(ordersInfo.createtime))
        $('.orderStatus').text(getStatus(ordersInfo.status))
        $('.name').text(ordersInfo.name)
        $('.phone').text(ordersInfo.phone)
        $('.address').text(ordersInfo.sheng + ordersInfo.shi + ordersInfo.qu + ordersInfo.address)
    }
})

function getStatus(status){
    let statusTxt = ''
    switch (status) {
        case '0':
        statusTxt = '待付款'
        break;
        case '1':
        statusTxt = '待发货'
        break;
        case '2':
        statusTxt = '待收货'
        break;
        case '3':
        statusTxt = '已完成'
        break;
        case '4':
        statusTxt = '已评价'
        break;
    }
    return statusTxt
}